package webserver;

import java.util.logging.*;
import java.io.*;

/**
 *
 * @author lucas
 */
public class EnhancedLogger extends Logger {

    /**
     * Constructor. Crea un Logger con el nombre pasado.
     * 
     * @param name 
     */
    private EnhancedLogger(String name) {
        super(name, null);
    }
    
    
    public static EnhancedLogger getLogger(String name) {
        LogManager manager = LogManager.getLogManager();
        EnhancedLogger result = new EnhancedLogger(name);
        assert manager.addLogger(result);
        return result;
    }
    
    /**
     * loguea un string formateado con nivel de info
     * 
     * @param format
     * @param args 
     */
    public void info(String format, Object[] args) {
        super.info(String.format(format, args));
    }
    
    /**
     * loguea el stacktrace de un objeto throwable con nivel de info.
     * 
     * @param aThrowable 
     */
    public void info(Throwable aThrowable) {
        super.info(stack2String(aThrowable));
    }
    
    /**
     * loguea el stacktrace de un objeto throwable con nivel de severe.
     * 
     * @param aThrowable 
     */
    public void severe(Throwable aThrowable) {
        super.severe(stack2String(aThrowable));
    }
    
    /**
     * convierte el stacktrace de un objeto throwable a string.
     * 
     * @param aThrowable
     * @return 
     */
    private String stack2String(Throwable aThrowable) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        aThrowable.printStackTrace(printWriter);
        return result.toString();
    }
    
}
